<?php
	class HtmlHelper extends GlobalHelper
	{
		//COMMON TAGS

	    /**
	     * Generates meta tag
	     *
	     * @param string $name meta name
	     * @param string $content content
	     * @param enum(name, http-equiv) $type content type
	     * @param $attributes $attributes other attributes
	     */	
		function MetaTag($name, $content, $type = 'name', $attributes = null)
		{
			echo "<meta $type = '$name' content = '$content' $attributes />\n";
		}

		/**
		 * Generates title tag
		 *
		 * @param unknown_type $title
		 * @param unknown_type $attributes
		 */
		function TitleTag($title, $attributes = null)
		{
			echo "<title $attributes>$title</title>\n";
		}
		
	    /**
		 * Generates link tag with css type
		 *
		 * @param string $filename css file name
		*/		
		function css($filename)
		{
			global $current_controller;
			echo "<link type=\"text/css\" rel=\"stylesheet\" href=\"/".THEMES.SEPARATOR.$current_controller->theme.SEPARATOR.$filename.".css\"/>";
		}

	    /**
		 * Generates link tag with js type
		 *
		 * @param string $filename js file name
		*/		
		function js($filename)
		{
			echo "<script type='text/javascript' src=\"/app/includes/js".SEPARATOR.$filename.".js\"></script>\n";
		}
		
	    /**
		 * Generates img tag
		 *
		 * @param string $src path to the image file
		 * @param integer $width image width
		 * @param integer $height image height
		 * @param string $alt image alernative text
		 * @param string $class image css class
		 * @param string $align image horizontal alignment
		 */		
		static function img($src, $width = null, $height = null, $alt = null, $class = null, $align = null)
		{
			$path = IMAGES.SEPARATOR;
			echo sprintf('<img src="%s%s" width="%d" height="%d" alt="%s" class="%s" align="%s" />', $path, $src, $width, $height, $alt, $class, $align);
		}
		
	    /**
		 * Generates a tag
		 *
		 * @param string $url url (href)
		 * @param string $text text
		 * @param string $htmlAttributes attributes for the tag
		 */		
		public function link($url, $text, $htmlAttributes = null)
		{	
			echo "<a href='$url' $htmlAttributes>$text</a>";
		}

		
		//FORM
	    /**
		 * Generates a open form tag
		 *
		 * @param string $action form action 
		 * @param string $method data transmission method - GET/POST (default: POST)
		 * @param string $htmlAttributes attributes for the tag (default: null)
		 */		
		static function form($action, $method='POST', $html_attributes=null)
		{
			echo "<form action='$action' method='$method' $html_attributes>";
		}
				
	    /**
		 * Generates a close form tag
		 */		
		static function formClose()
		{
			echo "</form>";
		}
		
	    /**
		 * Generates a combo box (select + options tags). Does not allow empty values
		 *
		 * @param string $name select name 
		 * @param array $assoc_array data array (value, text)
		 * @param integer $selected_id selected option id (default: null)
		 * @param string $htmlAttributes attributes for the tag (default: null)
		 */		
		public function combo($name, $assoc_array, $selected_id = null, $html_attributes = null)
		{
			echo sprintf('<select name="%s" %s onchange = "var text_id = \''.$name.'\'.replace(\'[\',\'_text[\');  if(text_id==\''.$name.'\') text_id = \''.$name.'\'+\'_text\';document.getElementById(text_id).value = this.options[selectedIndex].text">', $name, $html_attributes);
			foreach ($assoc_array as $v)
			{
				$value = array_shift($v);
				$text = array_shift($v);
				if (is_array($selected_id)) 
				{
					if (in_array($value, $selected_id)) $selected='selected="selected"';
					else $selected = "";
				}
				else
				{
					if ($value==$selected_id) $selected='selected="selected"';
					else $selected = "";
				}
				echo "\n\t".sprintf('<option value="%s" %s>%s</option>', $value, $selected, $text);
			}
			echo "\n".'</select>';
		}		
		
	    /**
		 * Generates a select box (select + options tags). Allows empty values
		 *
		 * @param string $name select name 
		 * @param array $assoc_array data array (value, text)
		 * @param integer $selected_id selected option id (default: null)
		 * @param string $htmlAttributes attributes for the tag (default: null)
		 */		
		static function select($name, $assoc_array, $selected_id = null, $html_attributes = null)
		{
			echo sprintf('<select name="%s" %s onchange = "var text_id = \''.$name.'\'.replace(\'[\',\'_text[\');  if(text_id==\''.$name.'\') text_id = \''.$name.'\'+\'_text\';document.getElementById(text_id).value = this.options[selectedIndex].text">', $name, $html_attributes);
			if ($selected_id == null || $selected_id == 0)
			{
				echo "<option value='' selected='selected'>-</option>";
			}
			else 
			{
				echo "<option value=''>-</option>";
			}
			foreach ($assoc_array as $v)
			{
				$value = array_shift($v);
				$text = array_shift($v);
				if ($value==$selected_id) $selected='selected="selected"';
				else $selected = "";
				echo "\n\t".sprintf('<option value="%s" %s>%s</option>', $value, $selected, $text);
			}
			echo "\n".'</select>';
		}
				
	    /**
		 * Generates an input field with the submit type
		 *
		 * @param string $name input field name 
		 * @param string $value the text to be displayed on the button
		 * @param string $htmlAttributes attributes for the tag (default: null)
		 */		
		static function submit($name, $value, $html_attributes=null)
		{
			echo "<input name='$name' type='submit' value='$value' $html_attributes />";
		}
		
	    /**
		 * Generates an input field with the reset type
		 *
		 * @param string $name input field name 
		 * @param string $value the text to be displayed on the button
		 * @param string $htmlAttributes attributes for the tag (default: null)
		 */		
		static function reset($name, $value, $html_attributes=null)
		{
			echo "<input name='$name' type='reset' value='$value' $html_attributes />";
		}
		
	    /**
		 * Generates an input field with the button type
		 *
		 * @param string $name input field name 
		 * @param string $value the text to be displayed on the button
		 * @param string $htmlAttributes attributes for the tag (default: null)
		 */		
		public function Button($name, $value, $html_attributes=null)
		{
			echo "<input name='$name' type='button' value='$value' $html_attributes />";
		}
		
	    /**
		 * Generates an input field with the cancel type
		 *
		 * @param string $url url to which a user should be redirected
		 * @param string $value the text to be displayed on the button
		 * @param string $htmlAttributes attributes for the tag (default: null)
		 * @param string $name input field name (default: cancel)
	 	*/		
		public function cancel($url, $text, $html_attributes=null, $name = 'cancel')
		{
			echo "<input name='$name' type='reset' value='$text' onclick='window.location.replace(\"$url\"); return false;' $html_attributes />";
		}
		
	    /**
		 * Generates an input field with the checkbox type
		 *
		 * @param string $name input field name
		 * @param string $value input value
		 * @param string $is_checked indicates whether the field in checked (default: 0)
		 * @param string $htmlAttributes attributes for the tag (default: null)
	 	*/		
		public function checkBox($name, $value, $is_checked = 0, $html_attributes = null)
		{
			if (!empty($is_checked)) $checked="checked='checked'";
			echo "<input name='".$name."' class = 'checkbox' type='checkbox' ".$checked." value='".$value."' ".$html_attributes." />";
		} 

	    /**
		 * Generates an input field with radio type
		 *
		 * @param string $name input name
		 * @param string $value input value
		 * @param string $label input field label
		 * @param string $is_checked indicates whether the radio is checked (default: 0)
		 * @param string $htmlAttributes attributes for the tag (default: null)
	 	*/		
		public function radio($name, $value, $label, $is_checked = 0, $html_attributes = null)
		{
			if (!empty($is_checked)) $checked="checked='checked'";
			echo "<input name='".$name."' type='radio' ".$checked." value='".$value."' ".$html_attributes." />$label";
		} 
		
	    /**
		 * Generates an input field with text type
		 *
		 * @param string $name input name
		 * @param string $value input value
		 * @param string $htmlAttributes attributes for the tag (default: null)
	 	*/		
		public function inputText($name, $value, $html_attributes = null)
		{
			$text = "<input name='".$name."' type='text' value='".$value."' ".$html_attributes." />";
			echo $text;
		}

	    /**
		 * Generates an input field with text image
		 *
		 * @param string $name input name
		 * @param string $value input value
		 * @param string $htmlAttributes attributes for the tag (default: null)
	 	*/		
		public function InputImage($name, $value, $html_attributes = null)
		{
			$text = "<input name='".$name."' type='image' src='".$value."' ".$html_attributes." />";
			echo $text;
		}
		
	    /**
		 * Generates an input field with password type
		 *
		 * @param string $name input name
		 * @param string $value input value
		 * @param string $htmlAttributes attributes for the tag (default: null)
	 	*/		
		public function password($name, $value, $html_attributes = null)
		{
			echo "<input name='".$name."' type='password' value='".$value."' ".$html_attributes." autocomplete = 'off' />";	
		}
		
	    /**
		 * Generates an input field with hidden type
		 *
		 * @param string $name input name
		 * @param string $value input value
	 	*/		
		public function hidden($name, $value)
		{
			echo "<input name='".$name."' id ='".$name."' type='hidden' value='".$value."'/>";	
		}
		
	    /**
		 * Generates a textarea tag
		 *
		 * @param string $name input name
		 * @param string $value input value
		 * @param string $htmlAttributes attributes for the tag (default: null)
	 	*/		
		public function Textarea($name, $value, $html_attributes = null)
		{
			echo "<textarea name='".$name."' ".$html_attributes.">".$value."</textarea>";	
		}

		public function inputFile($name, $value, $html_attributes = null)
		{
			echo "<input name='".$name."' id ='".$name."' type='file' value='".$value."' $html_attributes />";	
		}

		//TABLE
		//depricated. Used partially in htmlx helper.
		static function td($content, $html_attributes=null, $echo=0)
		{
			echo "<td $html_attributes>$content</td>";
		}
			
		static function tr($content, $html_attributes=null)
		{
			echo "<tr $html_attributes>$content</tr>";
		}
			
		static function trOpen($html_attributes=null)
		{
			echo "<tr $html_attributes>";
		}
		
		static function trClose()
		{
			echo '</tr>';
		}	
		
		static function tdOpen($html_attributes=null)
		{
			echo "<td $html_attributes>";
		}
		
		static function tdClose()
		{
			echo '</td>';
		}	
		
		static function tableOpen($html_attributes=null)
		{
			echo "<table $html_attributes>";
		}
		
		static function tableClose()
		{
			echo '</table>';
		}
		
		public function br()
		{
			echo "<br />";
		}
		
		
		//JAVASCRIPT
		
	    /**
		 * Generates a javascript <script> tag
		 *
		 * @param string $script js script
		 */		
		public function JavaScript($script)
		{
			echo "<script language='javascript'>";
			echo $script;
			echo "</script>";
		}

	    /**
		 * Generates a javascript alert
		 *
		 * @param string $text text to be alerted
		 */		
		public function Alert($text)
		{
			$this->JavaScript("alert('$text')");
		}
		
	}
?>